package com.haoziqi.chapter_05;

import com.haoziqi.chapter_02.WaterSensor;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * description
 * created by A on 2021/3/5
 */
public class MinByTrans {
    public static void main(String[] args) {
        //1.创建环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment().setParallelism(1);
        DataStreamSource<Integer> stream = env.fromElements(1, 2, 3, 4, 5);

        ArrayList<WaterSensor> waterSensors = new ArrayList<>();
        waterSensors.add(new WaterSensor("sensor_1", 1607527992000L, 20));
        waterSensors.add(new WaterSensor("sensor_1", 1607527994000L, 30));
        waterSensors.add(new WaterSensor("sensor_1", 1607527996000L, 30));
        waterSensors.add(new WaterSensor("sensor_2", 1607527993000L, 10));
        waterSensors.add(new WaterSensor("sensor_2", 1607527995000L, 30));
        //复习:keyedStream两个泛型分别表示Input数据类型和Key的类型
        KeyedStream<WaterSensor, String> ks = env.fromCollection(waterSensors).keyBy(new KeySelector<WaterSensor, String>() {
            @Override
            public String getKey(WaterSensor value) throws Exception {
                return value.getId();
            }
        });

        //聚合函数只能对keyby过的数据使用,因为都是keyStream里的方法
        ks.maxBy("vs",false).print("sum求和");
        //第一个参数,找到同一组最小的字段，第二个参数,设置false，除了排序字段，其他字段使用最新字段，设置true，6除了排序字段，其他字段使用组内第一条数据
        //ks.min("vs").print();
        //提交任务
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
